﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using PPI.Entities.Lookup;
using PPI.Data;

namespace PPIDataAccess.Distribution
{
    public class DTops
    {
        public static DataSet Select(string connectionKey)
        {
            SqlDataAdapter da = null;

            try
            {
                da = new SqlDataAdapter("up_tb_Vendor_Distribution_TopsXPoles_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception exc)
            {
                throw new Exception("Error executing up_tb_Vendor_Distribution_TopsXPoles_SelectAll", exc);
            }
            finally
            {
                if (da != null)
                    da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(EXMissionStructures_PoleTopType top, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_TopsXPole_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                #endregion

                #region Top

                if (top.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TopFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TopFk", ParameterDirection.Input, true, Convert.ToInt32(top.Key)));

                if (String.IsNullOrEmpty(top.Name))
                    cmd.Parameters.Add(DBHelper.CreateVarCharParameter("@Vendor_Distribution_TopName", 50, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateVarCharParameter("@Vendor_Distribution_TopName", 50, ParameterDirection.Input, true, top.Name));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionTop_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionTop_Id"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();

                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Update(List<EXMissionStructures_PoleTopType> tops, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_TopsXPole_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Delete actual tops 

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, poleId));

                cmd.ExecuteNonQuery();

                #endregion

                #region Insert new Tops

                foreach (EXMissionStructures_PoleTopType top in tops)
                {
                    cmd = new SqlCommand("up_tb_Vendor_Distribution_TopsXPole_Insert", tr.Connection);
                    cmd.Transaction = tr;

                    cmd.CommandType = CommandType.StoredProcedure;

                    if (poleId == Int32.MinValue)
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                    if (top.Id == Int32.MinValue)
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TopFk", ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TopFk", ParameterDirection.Input, true, top.Id));

                    if (String.IsNullOrEmpty(top.Name))
                        cmd.Parameters.Add(DBHelper.CreateVarCharParameter("@Vendor_Distribution_TopName", 50, ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateVarCharParameter("@Vendor_Distribution_TopName", 50, ParameterDirection.Input, true, top.Name));

                    cmd.ExecuteNonQuery();
                }

                #endregion

                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static DataSet SelectAll(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Distribution_Topss_SelectAll", PPI.Data.DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Distribution_Topss_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAll(string connectionKey, Int32 fromId)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Distribution_Topss_SelectAllFromId", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();

                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@fromId", ParameterDirection.Input, true, fromId));

                da.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Distribution_Topss_SelectAllFromId", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }
    }
}
